<template>
  <div class="product-list-container">
    <el-row justify="center">
      <el-col >
        <h2 class="section-title">Featured Products</h2>
        <el-row :gutter="20">
          <el-col 
            v-for="product in products" 
            :key="product.id"
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            class="mb-4"
          >
            <el-card 
              class="product-card" 
              :body-style="{ padding: '0px' }"
              shadow="hover"
            >
              <div class="image-wrapper">
                <el-image 
                  :src="product.image"
                  fit="cover"
                  class="product-image"
                  loading="lazy"
                >
                  <template #error>
                    <div class="image-error">
                      <el-icon><Picture /></el-icon>
                    </div>
                  </template>
                </el-image>
                <div class="hover-overlay">
                  <el-button 
                    type="primary" 
                    class="quick-view-btn"
                    @click="navigateToProduct(product.id)"
                  >
                    Quick View
                  </el-button>
                </div>
              </div>
              <div class="product-info">
                <h3 class="product-title" :title="product.title">{{ product.title }}</h3>
                <p class="product-description" :title="product.description">
                  {{ product.description }}
                </p>
                <div class="price-action">
                  <span class="product-price">${{ product.price.toFixed(2) }}</span>
                  <el-button 
                    type="primary" 
                    text
                    class="details-btn"
                    @click="navigateToProduct(product.id)"
                  >
                    View Details
                    <el-icon class="el-icon--right"><ArrowRight /></el-icon>
                  </el-button>
                </div>
                <div class="product-category">
                  <el-tag 
                    :type="getCategoryType(product.category)"
                    size="small"
                    effect="light"
                  >
                    {{ product.category.toUpperCase() }}
                  </el-tag>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useProductStore } from '@/stores/products'
import { Picture, ArrowRight } from '@element-plus/icons-vue'
import type { TagProps } from 'element-plus'

const router = useRouter()
const store = useProductStore()
const products = store.getAllProducts

const navigateToProduct = (productId: number) => {
  router.push(`/product/${productId}`)
}

const getCategoryType = (category: string): TagProps['type'] => {
  const types: Record<string, TagProps['type']> = {
    drone: 'success',
    toy: 'warning',
    car: 'info',
    robot: 'success'
  }
  return types[category] || 'info'
}
</script>

<style scoped>
.product-list-container {
  padding: 2rem 0;
  background-color: var(--el-bg-color-page);
}

.section-title {
  font-size: 2rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 2rem;
  color: var(--el-text-color-primary);
}

.product-card {
  height: 100%;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.image-wrapper {
  position: relative;
  padding-bottom: 100%;
  overflow: hidden;
}

.product-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image {
  transform: scale(1.05);
}

.hover-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.product-card:hover .hover-overlay {
  opacity: 1;
}

.quick-view-btn {
  transform: translateY(20px);
  transition: transform 0.3s ease;
}

.product-card:hover .quick-view-btn {
  transform: translateY(0);
}

.product-info {
  padding: 1rem;
}

.product-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 0.5rem;
  color: var(--el-text-color-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-description {
  font-size: 0.9rem;
  color: var(--el-text-color-secondary);
  margin-bottom: 1rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  height: 2.7rem;
}

.price-action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.product-price {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--el-color-danger);
}

.details-btn {
  font-size: 0.9rem;
}

.details-btn:hover .el-icon {
  transform: translateX(4px);
}

.details-btn .el-icon {
  transition: transform 0.2s ease;
}

.product-category {
  margin-top: 0.5rem;
}

.image-error {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-secondary);
  font-size: 2rem;
}

.mb-4 {
  margin-bottom: 1rem;
}

/* Responsive Adjustments */
@media (max-width: 768px) {
  .section-title {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
  }

  .product-title {
    font-size: 1rem;
  }

  .product-description {
    font-size: 0.8rem;
  }

  .product-price {
    font-size: 1rem;
  }

  .details-btn {
    font-size: 0.8rem;
  }
}
</style> 